﻿#region Namespace References

using System;
using System.Data;
using System.IO;
using System.Threading;
using System.Windows.Forms;
using System.Xml;
using DynConDevCorp.OpenArch.SmartData.Core;
using DynConDevCorp.OpenArch.SmartData.Interfaces;
using DynConDevCorp.OpenArch.SmartData.NorthWindLoader.NorthWindTableAdapters;
using DynConDevCorp.OpenArch.SmartData.Persistance;

#endregion

namespace DynConDevCorp.OpenArch.SmartData.NorthWindLoader
{
    public partial class Form1 : Form
    {
        #region Constructors

        public Form1()
        {
            InitializeComponent();
        }

        #endregion

        #region Private Instance Methods

#if false        
        private void button1_old__Click(object sender, EventArgs e)
        {
            button1.Enabled = false;
            LoadDataSet();
            SmartLoaderSettings loaderSettings = new SmartLoaderSettings();
            SmartLoader loader = new SmartLoader(loaderSettings);
            ISmartEntityCollection<SmartDynamicEntity> customers =
                loader.GetCollection<SmartDynamicEntity>(m_NorthWindDataSet.Customers);
            ISmartEntityCollection<SmartDynamicEntity> products =
                loader.GetCollection<SmartDynamicEntity>(m_NorthWindDataSet.Products);
            ISmartEntityCollection<SmartDynamicEntity> orders =
                loader.GetCollection<SmartDynamicEntity>(m_NorthWindDataSet.Orders);
            ISmartEntityCollection<SmartDynamicEntity> orderDetails =
                loader.GetCollection<SmartDynamicEntity>(m_NorthWindDataSet.Order_Details);
            SmartEntityInstanceGraph instanceGraph = new SmartEntityInstanceGraph();
            instanceGraph.AddCollection("customers", customers);
            instanceGraph.AddCollection("products", products);
            instanceGraph.AddCollection("orders", orders);
            instanceGraph.AddCollection("orderDetails", orderDetails);
            using (TextWriter textWriter = new StreamWriter("SmartNorthWind.xml"))
            {
                XmlWriterSettings settings = new XmlWriterSettings();
                settings.Indent = true;
                settings.NewLineHandling = NewLineHandling.Replace;
                using (XmlWriter xmlWriter = XmlWriter.Create(textWriter, settings))
                {
                    SmartGraphWriterXml graphWriter = new SmartGraphWriterXml(xmlWriter);
                    graphWriter.Write(instanceGraph);
                }
            }

            button1.Enabled = true;
        }
#endif
        /// <summary>
        /// Handles the Click event of the button1 control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void button1_Click(object sender, EventArgs e)
        {
            button1.Enabled = false;
            LoadDataSet();
            SmartLoaderSettings loaderSettings = new SmartLoaderSettings();
            loaderSettings.Register<Customer>("customers");
            loaderSettings.Register<Product>("products");
            loaderSettings.Register<Order>("orders");
            loaderSettings.Register<OrderDetail>("order details");
            loaderSettings.Register<Order, Customer>("CustomerCode", "CustomerCode", "Customer");
            loaderSettings.Register<OrderDetail, Order>("OrderNumber", "OrderNumber", "Order");
            loaderSettings.Register<OrderDetail, Product>("ProductNumber", "ProductNumber", "Product");
            SmartLoader loader = new SmartLoader(loaderSettings) { DataSource = m_NorthWindDataSet };

            ISmartEntityCollection<Customer> customers = loader.GetCollection<Customer>();
            ISmartEntityCollection<Product> products = loader.GetCollection<Product>();
            ISmartEntityCollection<Order> orders = loader.GetCollection<Order>();
            ISmartEntityCollection<OrderDetail> orderDetails = loader.GetCollection<OrderDetail>();


            ////ISmartEntityCollection<Customer> customers =
            ////     loader.LoadTable<Customer>(m_NorthWindDataSet.Customers);
            ////ISmartEntityCollection<Product> products =
            ////    loader.LoadTable<Product>(m_NorthWindDataSet.Products);
            ////ISmartEntityCollection<Order> orders =
            ////    loader.LoadTable<Order>(m_NorthWindDataSet.Orders);
            ////ISmartEntityCollection<OrderDetail> orderDetails =
            ////    loader.LoadTable<OrderDetail>(m_NorthWindDataSet.Order_Details);

            ////button1.Enabled = true;
            ////return;

            SmartEntityInstanceGraph instanceGraph = new SmartEntityInstanceGraph();
            instanceGraph.AddCollection("customers", customers);
            instanceGraph.AddCollection("products", products);
            instanceGraph.AddCollection("orders", orders);
            instanceGraph.AddCollection("orderDetails", orderDetails);
            using (TextWriter textWriter = new StreamWriter("SmartNorthWind.xml"))
            {
                XmlWriterSettings settings = new XmlWriterSettings();
                settings.Indent = true;
                settings.NewLineHandling = NewLineHandling.Replace;
                using (XmlWriter xmlWriter = XmlWriter.Create(textWriter, settings))
                {
                    SmartGraphWriterXml graphWriter = new SmartGraphWriterXml(xmlWriter);
                    graphWriter.Write(instanceGraph);
                }
            }

            button1.Enabled = true;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            button2.Enabled = false;
            SmartEntityInstanceGraph objectGraph;
            using (TextReader textReader = new StreamReader("SmartNorthWind.xml"))
            {
                XmlReaderSettings settings = new XmlReaderSettings();
                settings.IgnoreComments = true;
                settings.IgnoreWhitespace = true;
                using (XmlReader xmlReader = XmlReader.Create(textReader, settings))
                {
                    SmartGraphReaderXml graphReader = new SmartGraphReaderXml(xmlReader);
                    objectGraph = graphReader.ReadAll();
                }
            }
            ////ISmartEntityCollection<SmartDynamicEntity> rawCustomers = objectGraph.GetCollection<SmartDynamicEntity>("customers");
            ////ISmartEntityCollection<SmartDynamicEntity> rawProducts = objectGraph.GetCollection<SmartDynamicEntity>("products");
            ////ISmartEntityCollection<SmartDynamicEntity> rawOrders = objectGraph.GetCollection<SmartDynamicEntity>("orders");
            ////ISmartEntityCollection<SmartDynamicEntity> rawOrderDetails = objectGraph.GetCollection<SmartDynamicEntity>("orderDetails");

            ////SmartEntityTransform transform = new SmartEntityTransform();
            ////ISmartEntityCollection<Customer> customers = transform.Transform<SmartDynamicEntity, Customer>(rawCustomers);
            ////ISmartEntityCollection<Product> products = transform.Transform<SmartDynamicEntity, Product>(rawProducts);
            ////ISmartEntityCollection<Order> orders = transform.Transform<SmartDynamicEntity, Order>(rawOrders);
            ////ISmartEntityCollection<OrderDetail> orderDetails = transform.Transform<SmartDynamicEntity, OrderDetail>(rawOrderDetails);

            ISmartEntityCollection<Customer> customers = objectGraph.GetCollection<Customer>("customers");
            ISmartEntityCollection<Product> products = objectGraph.GetCollection<Product>("products");
            ISmartEntityCollection<Order> orders = objectGraph.GetCollection<Order>("orders");
            ISmartEntityCollection<OrderDetail> orderDetails = objectGraph.GetCollection<OrderDetail>("orderDetails");

            button2.Enabled = true;
        }

        private void LoadDataSet()
        {
            m_NorthWindDataSet = new NorthWind();
            CustomersTableAdapter cta = new CustomersTableAdapter();
            cta.Fill(m_NorthWindDataSet.Customers);
            ProductsTableAdapter pta = new ProductsTableAdapter();
            pta.Fill(m_NorthWindDataSet.Products);
            OrdersTableAdapter ota = new OrdersTableAdapter();
            ota.Fill(m_NorthWindDataSet.Orders);
            Order_DetailsTableAdapter odta = new Order_DetailsTableAdapter();
            odta.Fill(m_NorthWindDataSet.Order_Details);
            DataRelationCollection relations = m_NorthWindDataSet.Relations;
            foreach (DataRelation relation in relations)
            {
                int x = 1;
            }
        }

        #endregion

        #region Fields and Constants

        private NorthWind m_NorthWindDataSet;

        #endregion

        private void button3_Click(object sender, EventArgs e)
        {
            button3.Enabled = false;
            button1_Click(sender, e);
            Thread.Sleep(TimeSpan.FromSeconds(15));
            button2_Click(sender, e);
            button3.Enabled = true;
        }

     }
}